<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ArticleComment;
use App\Models\Article;
use App\Models\Category;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ArticleCommentController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ArticleComment(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('category_id','所属类别')->width(180)->display(function ($category_id){
                $category = Category::where('id',$category_id)->first();
                if($category){
                    return $category->name;
                }
                    return "不知名类别";
            })->label('#586cb1');

            $grid->column('article_id','文章名称')->display(function ($article_id) {
                $article = Article::where('id',$article_id)->first();
                return $article->title;
            });
            $grid->column('nickname','用户昵称')->width(240);
            $grid->column('content','评论内容')->width(300);
            $grid->column('created_at','评论时间');

            $grid->disableCreateButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('article_id','文章id');
                $filter->like('content','评论内容');
                $filter->like('date','评论日期');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ArticleComment(), function (Show $show) {
            $show->field('id');
            $show->field('article_id');
            $show->field('user_id');
            $show->field('nickname');
            $show->field('content');
            $show->field('date');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ArticleComment(), function (Form $form) {
            $form->display('id');
            $form->text('article_id');
            $form->text('user_id');
            $form->text('nickname');
            $form->text('content');
            $form->text('date');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
